/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.data.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.framework.yyfax.enums.YesNoEnum;
import com.yyfax.pay.order.data.dao.UserPaymentDao;
import com.yyfax.pay.order.model.po.UserPaymentPO;

/**
 * 用户支付信息持久化实现
 *
 * @author buyi
 * @date 2017年04月12日 11:09:15
 * @since 1.0.0
 */
@Repository
public class UserPaymentDaoImpl extends BaseDaoImpl<UserPaymentPO, Integer> implements UserPaymentDao {
	/** 表名，该值要和user_payment.xml的属性"tableName"保持一致 */
	private static final String TABLE_NAME = "user_payment";

	/** 方法名，该值要和user_payment.xml的属性"id"保持一致 */
	private final String SQL_INSERT = "insert";

	/** 方法名，该值该值要和user_payment.xml的属性"id"保持一致 */
	private final String SQL_SELECT_BY_ID = "selectById";

	/** 方法名，该值该值要和user_payment.xml的属性"id"保持一致 */
	private final String SQL_SELECT_LIST_BY_USER_ID_MERCHANT_CODE = "selectListByUserIdAndMerchantCode";

	private final String SQL_UPDATE = "update";

	private final String SQL_SELECT_BY_MERCHANTCODE_AND_USERID_PAYMENTCODE = "selectByMerchantCodeAndUserIdAndPaymentCode";

	private final String SQL_DELETE = "delete";

	private final String SQL_UPDATE_PHONE = "updatePhone";
	
	private final String SQL_UPDATE_PHONE_BY_MERCHANT_AND_USERID_AND_PAYMENT = "updatePhoneByMerchantAndUserIdAndPayment";

	public UserPaymentDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insert(UserPaymentPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}

	@Override
	public UserPaymentPO selectByIdAndMerchantCode(int id, String merchantCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);
		return namedParameterJdbcTemplate.queryForObject(sql, params,
				new BeanPropertyRowMapper<UserPaymentPO>(poClass));
	}

	@Override
	public List<UserPaymentPO> selectListByUserIdAndMerchantCode(String userId, String merchantCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_LIST_BY_USER_ID_MERCHANT_CODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("userId", userId);
		params.addValue("merchantCode", merchantCode);
		return namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<UserPaymentPO>(poClass));
	}

	@Override
	public int update(UserPaymentPO po){
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}

	@Override
	public UserPaymentPO selectByMerchantCodeAndUserIdAndPaymentCode(String merchantCode, String userId,
			String paymentCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCHANTCODE_AND_USERID_PAYMENTCODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("userId", userId);
		params.addValue("paymentCode", paymentCode);
		return namedParameterJdbcTemplate.queryForObject(sql, params,
				new BeanPropertyRowMapper<UserPaymentPO>(poClass));
	}

	@Override
	public int delete(String merchantCode, String userId, String paymentCode, String cardNo) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_DELETE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("userId", userId);
		params.addValue("paymentCode", paymentCode);
		params.addValue("cardNo", cardNo);
		params.addValue("state", YesNoEnum.NO.getValue());

		return namedParameterJdbcTemplate.update(sql, params);
	}

	@Override
	public int updatePhone(int id, String phone) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE_PHONE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);
		params.addValue("phone", phone);

		return namedParameterJdbcTemplate.update(sql, params);
	}
	
	@Override
	public int updatePhoneByMerchantAndUserIdAndPayment(String merchantCode,String userId,String paymentCode,String phone){
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE_PHONE_BY_MERCHANT_AND_USERID_AND_PAYMENT);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("userId", userId);
		params.addValue("paymentCode", paymentCode);
		params.addValue("phone", phone);

		return namedParameterJdbcTemplate.update(sql, params);
	}

}
